﻿@namespace Oqtane.Modules.Admin.Themes
@using System.Net
@inherits ModuleBase
@inject NavigationManager NavigationManager
@inject IThemeService ThemeService
@inject IPackageService PackageService

@if (_themes == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <ActionLink Action="Add" Text="Install Theme" />

    <Pager Items="@_themes">
        <Header>
            <th style="width: 1px;">&nbsp;</th>
            <th style="width: 1px;">&nbsp;</th>
            <th scope="col">Name</th>
            <th scope="col">Version</th>
            <th>&nbsp;</th>
        </Header>
        <Row>
            <td><ActionLink Action="View" Parameters="@($"name=" + WebUtility.UrlEncode(context.ThemeName))" /></td>
            <td>
                @if (context.AssemblyName != "Oqtane.Client")
                    {
                    <ActionDialog Header="Delete Theme" Message="@("Are You Sure You Wish To Delete The " + context.Name + " Theme?")" Action="Delete" Security="SecurityAccessLevel.Host" Class="btn btn-danger" OnClick="@(async () => await DeleteTheme(context))" />
                    }
            </td>
            <td>@context.Name</td>
            <td>@context.Version</td>
            <td>
                @if (UpgradeAvailable(context.ThemeName, context.Version))
                    {
                    <button type="button" class="btn btn-success" @onclick=@(async () => await DownloadTheme(context.ThemeName, context.Version))>Upgrade</button>
                    }
            </td>
            <td></td>
        </Row>
    </Pager>
}

@code {
    private List<Theme> _themes;
    private List<Package> _packages;

    public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Host;

    protected override async Task OnInitializedAsync()
    {
        try
        {
            _themes = await ThemeService.GetThemesAsync();
            _packages = await PackageService.GetPackagesAsync("theme");
        }
        catch (Exception ex)
        {
            if (_themes == null)
            {
                await logger.LogError(ex, "Error Loading Themes {Error}", ex.Message);
                AddModuleMessage("Error Loading Themes", MessageType.Error);
            }
        }
    }

    private bool UpgradeAvailable(string themename, string version)
    {
        var upgradeavailable = false;
        if (_packages != null)
        {
            var package = _packages.Where(item => item.PackageId == Utilities.GetTypeName(themename)).FirstOrDefault();
            if (package != null)
            {
                upgradeavailable = (Version.Parse(package.Version).CompareTo(Version.Parse(version)) > 0);
            }
        }
        return upgradeavailable;
    }

    private async Task DownloadTheme(string themename, string version)
    {
        try
        {
            await PackageService.DownloadPackageAsync(themename, version, "Themes");
            await logger.LogInformation("Theme Downloaded {ThemeName} {Version}", themename, version);
            ShowProgressIndicator();
            var interop = new Interop(JSRuntime);
            await interop.RedirectBrowser(NavigateUrl(), 10);
            await ThemeService.InstallThemesAsync();
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Downloading Theme {ThemeName} {Version} {Error}", themename, version, ex.Message);
            AddModuleMessage("Error Downloading Theme", MessageType.Error);
        }
    }

    private async Task DeleteTheme(Theme Theme)
    {
        try
        {
            ShowProgressIndicator();
            var interop = new Interop(JSRuntime);
            await interop.RedirectBrowser(NavigateUrl(), 10);
            await ThemeService.DeleteThemeAsync(Theme.ThemeName);
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Deleting Theme {Theme} {Error}", Theme, ex.Message);
            AddModuleMessage("Error Deleting Theme", MessageType.Error);
        }
    }
}